package com.luckcoder.relax.games.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.luckcoder.relax.games.domain.bo.RelaxUserGameSettingsBo;
import com.luckcoder.relax.games.domain.vo.RelaxUserGameSettingsVo;
import com.luckcoder.relax.games.domain.RelaxUserGameSettings;
import com.luckcoder.relax.games.mapper.RelaxUserGameSettingsMapper;
import com.luckcoder.relax.games.service.IRelaxUserGameSettingsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户游戏设置Service业务层处理
 *
 * @author zhanghao
 * @date 2025-09-11
 */
@RequiredArgsConstructor
@Service
public class RelaxUserGameSettingsServiceImpl implements IRelaxUserGameSettingsService {

    private final RelaxUserGameSettingsMapper baseMapper;

    /**
     * 查询用户游戏设置
     */
    @Override
    public RelaxUserGameSettingsVo queryById(Long settingId) {
        return baseMapper.selectVoById(settingId);
    }

    /**
     * 根据用户ID和游戏编码查询设置
     */
    @Override
    public RelaxUserGameSettingsVo queryByUserAndGame(Long userId, String gameCode) {
        LambdaQueryWrapper<RelaxUserGameSettings> lqw = Wrappers.lambdaQuery();
        lqw.eq(RelaxUserGameSettings::getUserId, userId);
        lqw.eq(RelaxUserGameSettings::getGameCode, gameCode);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询用户游戏设置列表
     */
    @Override
    public TableDataInfo<RelaxUserGameSettingsVo> queryPageList(RelaxUserGameSettingsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RelaxUserGameSettings> lqw = buildQueryWrapper(bo);
        Page<RelaxUserGameSettingsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户游戏设置列表
     */
    @Override
    public List<RelaxUserGameSettingsVo> queryList(RelaxUserGameSettingsBo bo) {
        LambdaQueryWrapper<RelaxUserGameSettings> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RelaxUserGameSettings> buildQueryWrapper(RelaxUserGameSettingsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RelaxUserGameSettings> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, RelaxUserGameSettings::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getGameCode()), RelaxUserGameSettings::getGameCode, bo.getGameCode());
        lqw.eq(bo.getGameTime() != null, RelaxUserGameSettings::getGameTime, bo.getGameTime());
        lqw.eq(bo.getEmojiSpeed() != null, RelaxUserGameSettings::getEmojiSpeed, bo.getEmojiSpeed());
        lqw.eq(bo.getMaxEmojis() != null, RelaxUserGameSettings::getMaxEmojis, bo.getMaxEmojis());
        lqw.eq(bo.getEmojiSizeMin() != null, RelaxUserGameSettings::getEmojiSizeMin, bo.getEmojiSizeMin());
        lqw.eq(bo.getEmojiSizeMax() != null, RelaxUserGameSettings::getEmojiSizeMax, bo.getEmojiSizeMax());
        lqw.eq(bo.getAutoPopTime() != null, RelaxUserGameSettings::getAutoPopTime, bo.getAutoPopTime());
        return lqw;
    }

    /**
     * 新增用户游戏设置
     */
    @Override
    public Boolean insertByBo(RelaxUserGameSettingsBo bo) {
        RelaxUserGameSettings add = MapstructUtils.convert(bo, RelaxUserGameSettings.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSettingId(add.getSettingId());
        }
        return flag;
    }

    /**
     * 修改用户游戏设置
     */
    @Override
    public Boolean updateByBo(RelaxUserGameSettingsBo bo) {
        RelaxUserGameSettings update = MapstructUtils.convert(bo, RelaxUserGameSettings.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存用户游戏设置（存在则更新，不存在则新增）
     */
    @Override
    public Boolean saveUserGameSettings(RelaxUserGameSettingsBo bo) {
        RelaxUserGameSettingsVo existingSetting = queryByUserAndGame(bo.getUserId(), bo.getGameCode());
        if (existingSetting != null) {
            bo.setSettingId(existingSetting.getSettingId());
            return updateByBo(bo);
        } else {
            return insertByBo(bo);
        }
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RelaxUserGameSettings entity) {
        // 确保表情最大大小不小于最小大小
        if (entity.getEmojiSizeMin() != null && entity.getEmojiSizeMax() != null 
            && entity.getEmojiSizeMax() < entity.getEmojiSizeMin()) {
            throw new RuntimeException("表情最大大小不能小于最小大小");
        }
    }

    /**
     * 批量删除用户游戏设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}